package bb.lanxing.lib.devices.core.sync;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import java.lang.ref.WeakReference;
import java.util.Iterator;

public class SyncHelper implements ServiceConnection {
    private String address;
    private Bundle arguments;
    private WeakReference<SyncHelperCallback> syncHelperCallbackRef;
    private SyncManager syncManager;
    private String syncManagerName;
    private ComponentName syncService;


    public interface SyncHelperCallback {
        void onAttachedToSyncManager(SyncManager syncManager);

        void onDetachedFromSyncManager(SyncManager syncManager);
    }

    public SyncHelper(ComponentName componentName, String str, String str2, Bundle bundle, SyncHelperCallback syncHelperCallback) {
        this.syncService = componentName;
        this.address = str;
        this.syncManagerName = str2;
        this.arguments = bundle;
        this.syncHelperCallbackRef = new WeakReference<>(syncHelperCallback);
    }

    public SyncHelper(ComponentName componentName, String str, String str2, SyncHelperCallback syncHelperCallback) {
        this(componentName, str, str2, null, syncHelperCallback);
    }

    public String getAddress() {
        return this.address;
    }

    public String getSyncManagerName() {
        return this.syncManagerName;
    }

    public SyncManager getSyncManager() {
        return this.syncManager;
    }

    public boolean isSynchronising() {
        String str = this.address;
        return str != null && isSynchronising(this.syncManagerName, str);
    }

    public void bindSyncService(Context context) {
        if (this.address == null) {
            return;
        }
        Intent intent = new Intent();
        intent.setComponent(this.syncService);
        context.bindService(intent, this, 1);
    }

    public void startForgroundSyncService(Context context) {
        Intent intent = new Intent();
        intent.setComponent(this.syncService);
        if (Build.VERSION.SDK_INT >= 26) {
            context.startForegroundService(intent);
        } else {
            context.startService(intent);
        }
    }

    public void unbindSyncService(Context context, boolean z) {
        if (this.syncManager == null) {
            return;
        }
        Intent intent = new Intent();
        intent.setComponent(this.syncService);
        if (z) {
            context.startService(intent);
        } else {
            this.syncManager.release();
            if (!hasSynchronising()) {
                context.stopService(intent);
            }
        }
        this.syncManager = null;
        Intent intent2 = new Intent();
        intent2.setComponent(this.syncService);
        context.stopService(intent2);
    }

    @Override
    public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
        SyncManager syncManager;
        this.syncManager = ((SyncManagerFactory) iBinder).create(this.syncManagerName, this.address, this.arguments);
        WeakReference<SyncHelperCallback> weakReference = this.syncHelperCallbackRef;
        SyncHelperCallback syncHelperCallback = weakReference != null ? weakReference.get() : null;
        if (syncHelperCallback == null || (syncManager = this.syncManager) == null) {
            return;
        }
        syncHelperCallback.onAttachedToSyncManager(syncManager);
    }

    @Override
    public void onServiceDisconnected(ComponentName componentName) {
        if (this.syncManager != null) {
            this.syncManager = null;
        }
    }

    public static boolean isSynchronising(String str) {
        SyncManagerHolder syncManagerHolder;
        if (SyncService.instance == null || (syncManagerHolder = SyncService.instance.syncManagerHolderMap.get(str)) == null) {
            return false;
        }
        Iterator<SyncManager> it = syncManagerHolder.iterator();
        while (it.hasNext()) {
            if (it.next().isSynchronising()) {
                return true;
            }
        }
        return false;
    }

    public static boolean isSynchronising(String str, String str2) {
        if (SyncService.instance == null || str2 == null) {
            return false;
        }
        SyncManagerHolder syncManagerHolder = SyncService.instance.syncManagerHolderMap.get(str2);
        SyncManager syncManger = syncManagerHolder != null ? syncManagerHolder.getSyncManger(str) : null;
        return syncManger != null && syncManger.isSynchronising();
    }

    public static boolean hasSynchronising() {
        if (SyncService.instance == null) {
            return false;
        }
        for (SyncManagerHolder syncManagerHolder : SyncService.instance.syncManagerHolderMap.values()) {
            Iterator<SyncManager> it = syncManagerHolder.iterator();
            while (it.hasNext()) {
                if (it.next().isSynchronising()) {
                    return true;
                }
            }
        }
        return false;
    }

    public static Object getTag(String str, String str2) {
        SyncManagerHolder syncManagerHolder;
        SyncManager syncManger;
        if (SyncService.instance == null || str2 == null || (syncManagerHolder = SyncService.instance.syncManagerHolderMap.get(str2)) == null || (syncManger = syncManagerHolder.getSyncManger(str)) == null) {
            return null;
        }
        return syncManger.getTag();
    }
}
